package com.example.choupaiapp.main.activity.mine;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.choupaiapp.R;
import com.example.choupaiapp.common.adapter.BaseAdapterHelper;
import com.example.choupaiapp.common.adapter.QuickAdapter;
import com.example.choupaiapp.common.constant.Contacts;
import com.example.choupaiapp.common.finalokgo.callback.Callback;
import com.example.choupaiapp.common.finalokgo.request.RequestType;
import com.example.choupaiapp.common.ui.activity.CommonActivity;
import com.example.choupaiapp.common.util.SharedPreferencesHelp;
import com.example.choupaiapp.common.util.Utils;
import com.example.choupaiapp.main.activity.home.OtherInfoActivity;
import com.example.choupaiapp.main.bean.FollowFansBean;
import com.example.choupaiapp.main.bean.SendCodeBean;
import com.example.choupaiapp.main.bean.UserInfoBean;
import com.example.choupaiapp.main.constant.Config;
import com.example.choupaiapp.main.constant.Url;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by ASUS on 2019/11/25.
 */

public class FollowFasActivity extends CommonActivity implements OnRefreshLoadMoreListener {

    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private String flag = "";
    private QuickAdapter<FollowFansBean.PageBean.ListBean> quickAdapter;
    private UserInfoBean mUserInfoBean;
    private int pageNum = 1;
    private int pageSize = 10;
    @BindView(R.id.empty_img)
    ImageView empty_img;
    private String userid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_follow_fans);
    }

    @Override
    protected void initView() {

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            flag = bundle.getString("flag");
            if (flag.equals("mine_follow")) {
                setTitle("关注");
            } else if (flag.equals("mine_fans")) {
                setTitle("粉丝");
            } else if (flag.equals("other_follow")) {
                userid = bundle.getString("userid");
                setTitle("TA的关注");
            } else if (flag.equals("other_fans")) {
                userid = bundle.getString("userid");
                setTitle("TA的粉丝");
            }
        }
        refreshLayout.setOnRefreshLoadMoreListener(this);
        refreshLayout.setRefreshHeader(new MaterialHeader(context).setColorSchemeResources(R.color.common_color));

        mUserInfoBean = (UserInfoBean) SharedPreferencesHelp.getBean("userInfo");

        quickAdapter = new QuickAdapter<FollowFansBean.PageBean.ListBean>(context, R.layout.item_follow_fans) {
            @Override
            protected void convert(BaseAdapterHelper helper, FollowFansBean.PageBean.ListBean item) {
                LinearLayout item_ll = helper.getView(R.id.item_ll);
                CircleImageView head_photo = helper.getView(R.id.head_photo);
                Glide.with(context)
                        .load(item.getAvatar() + Config.IMAGE_LIM)
                        .dontAnimate()
                        .placeholder(R.mipmap.icon_no_head)
                        .into(head_photo);

                helper.setText(R.id.name, item.getNickname());

                TextView follow = helper.getView(R.id.follow);
                if (flag.equals("mine_follow")) {
                    head_photo.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (Utils.isDoubleClick()) {
                                return;
                            }
                            Bundle bundle1 = new Bundle();
                            bundle1.putString("userId", item.getByUserId() + "");
                            bundle1.putString("userName", item.getNickname() + "");
                            startActivity(OtherInfoActivity.class, bundle1);
                        }
                    });
                    item_ll.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (Utils.isDoubleClick()) {
                                return;
                            }
                            Bundle bundle1 = new Bundle();
                            bundle1.putString("userId", item.getByUserId() + "");
                            bundle1.putString("userName", item.getNickname() + "");
                            startActivity(OtherInfoActivity.class, bundle1);
                        }
                    });
                    if (item.getIsAttention() == 0) {
                        follow.setText("已关注");
                        follow.setBackgroundResource(R.drawable.shape_gray_oval);
                    } else {
                        follow.setText("互相关注");
                        follow.setBackgroundResource(R.drawable.shape_gray_oval);
                    }
                    follow.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (Utils.isDoubleClick()) {
                                return;
                            }
                            showDialog(context, "取消关注...");
                            finalOkGo.requestHead(RequestType.GETHEAD, Url.CANCEL_FOLLOW + item.getByUserId(), null, Url.CANCEL_FOLLOW, new Callback<SendCodeBean>() {
                                @Override
                                public void onSuccess(SendCodeBean sendCodeBean) {
                                    dismissDialog();
                                    if (sendCodeBean.getCode() == Contacts.SUCCESS) {
                                        quickAdapter.remove(helper.getPosition());
                                        quickAdapter.notifyDataSetChanged();
                                        EventBus.getDefault().post("refresh_home_follow");
                                        EventBus.getDefault().post("refresh_mine");
                                    } else {
                                        if (sendCodeBean.getCode() == Contacts.TOKEN_EMPTY) {
                                        } else {
                                            showImageToas(sendCodeBean.getMsg());
                                        }
                                    }
                                }

                                @Override
                                public void onError(Response<String> response) {
                                    super.onError(response);
                                    dismissDialog();
                                }
                            });
                        }
                    });
                } else if (flag.equals("mine_fans")) {
                    head_photo.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (Utils.isDoubleClick()) {
                                return;
                            }
                            Bundle bundle1 = new Bundle();
                            bundle1.putString("userId", item.getUserId() + "");
                            bundle1.putString("userName", item.getNickname() + "");
                            startActivity(OtherInfoActivity.class, bundle1);
                        }
                    });
                    item_ll.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (Utils.isDoubleClick()) {
                                return;
                            }
                            Bundle bundle1 = new Bundle();
                            bundle1.putString("userId", item.getUserId() + "");
                            bundle1.putString("userName", item.getNickname() + "");
                            startActivity(OtherInfoActivity.class, bundle1);
                        }
                    });
                    if (item.getIsAttention() == 0) {
                        follow.setText("关注TA");
                        follow.setBackgroundResource(R.drawable.shape_yellow_oval);
                        follow.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (Utils.isDoubleClick()) {
                                    return;
                                }
                                showDialog(context, "关注中...");
                                finalOkGo.requestHead(RequestType.GETHEAD, Url.FOLLOW_OTHER + item.getUserId(), null, Url.FOLLOW_OTHER, new Callback<SendCodeBean>() {
                                    @Override
                                    public void onSuccess(SendCodeBean sendCodeBean) {
                                        dismissDialog();
                                        if (sendCodeBean.getCode() == Contacts.SUCCESS) {
                                            item.setIsAttention(1);
                                            quickAdapter.notifyDataSetChanged();
                                            EventBus.getDefault().post("refresh_home_follow");
                                            EventBus.getDefault().post("refresh_mine");
                                        } else {
                                            if (sendCodeBean.getCode() == Contacts.TOKEN_EMPTY) {
                                            } else {
                                                showImageToas(sendCodeBean.getMsg());
                                            }
                                        }
                                    }

                                    @Override
                                    public void onError(Response<String> response) {
                                        super.onError(response);
                                        dismissDialog();
                                    }
                                });
                            }
                        });
                    } else {
                        follow.setText("互相关注");
                        follow.setBackgroundResource(R.drawable.shape_gray_oval);
                        follow.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (Utils.isDoubleClick()) {
                                    return;
                                }
                                showDialog(context, "取消关注...");
                                finalOkGo.requestHead(RequestType.GETHEAD, Url.CANCEL_FOLLOW + item.getUserId(), null, Url.CANCEL_FOLLOW, new Callback<SendCodeBean>() {
                                    @Override
                                    public void onSuccess(SendCodeBean sendCodeBean) {
                                        dismissDialog();
                                        if (sendCodeBean.getCode() == Contacts.SUCCESS) {
                                            item.setIsAttention(0);
                                            quickAdapter.notifyDataSetChanged();
                                            EventBus.getDefault().post("refresh_home_follow");
                                            EventBus.getDefault().post("refresh_mine");
                                        } else {
                                            if (sendCodeBean.getCode() == Contacts.TOKEN_EMPTY) {
                                            } else {
                                                showImageToas(sendCodeBean.getMsg());
                                            }
                                        }
                                    }

                                    @Override
                                    public void onError(Response<String> response) {
                                        super.onError(response);
                                        dismissDialog();
                                    }
                                });
                            }
                        });
                    }
                } else if (flag.equals("other_fans")) {
                    head_photo.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (Utils.isDoubleClick()) {
                                return;
                            }
                            Bundle bundle1 = new Bundle();
                            bundle1.putString("userId", item.getUserId() + "");
                            bundle1.putString("userName", item.getNickname() + "");
                            startActivity(OtherInfoActivity.class, bundle1);
                        }
                    });
                    item_ll.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (Utils.isDoubleClick()) {
                                return;
                            }
                            Bundle bundle1 = new Bundle();
                            bundle1.putString("userId", item.getUserId() + "");
                            bundle1.putString("userName", item.getNickname() + "");
                            startActivity(OtherInfoActivity.class, bundle1);
                        }
                    });
                    if (item.getIsAttention() == 0) {
                        follow.setText("关注TA");
                        follow.setBackgroundResource(R.drawable.shape_yellow_oval);

                        follow.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (Utils.isDoubleClick()) {
                                    return;
                                }
                                showDialog(context, "关注中...");
                                finalOkGo.requestHead(RequestType.GETHEAD, Url.FOLLOW_OTHER + item.getUserId(), null, Url.FOLLOW_OTHER, new Callback<SendCodeBean>() {
                                    @Override
                                    public void onSuccess(SendCodeBean sendCodeBean) {
                                        dismissDialog();
                                        if (sendCodeBean.getCode() == Contacts.SUCCESS) {
                                            item.setIsAttention(1);
                                            quickAdapter.notifyDataSetChanged();
                                            EventBus.getDefault().post("refresh_home_follow");
                                            EventBus.getDefault().post("refresh_mine");
                                        } else {
                                            if (sendCodeBean.getCode() == Contacts.TOKEN_EMPTY) {
                                            } else {
                                                showImageToas(sendCodeBean.getMsg());
                                            }
                                        }
                                    }

                                    @Override
                                    public void onError(Response<String> response) {
                                        super.onError(response);
                                        dismissDialog();
                                    }
                                });
                            }
                        });

                    } else {
                        follow.setText("已关注");
                        follow.setBackgroundResource(R.drawable.shape_gray_oval);

                        follow.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (Utils.isDoubleClick()) {
                                    return;
                                }
                                showDialog(context, "取消关注...");
                                finalOkGo.requestHead(RequestType.GETHEAD, Url.CANCEL_FOLLOW + item.getUserId(), null, Url.CANCEL_FOLLOW, new Callback<SendCodeBean>() {
                                    @Override
                                    public void onSuccess(SendCodeBean sendCodeBean) {
                                        dismissDialog();
                                        if (sendCodeBean.getCode() == Contacts.SUCCESS) {
                                            item.setIsAttention(0);
                                            quickAdapter.notifyDataSetChanged();
                                            EventBus.getDefault().post("refresh_home_follow");
                                            EventBus.getDefault().post("refresh_mine");
                                        } else {
                                            if (sendCodeBean.getCode() == Contacts.TOKEN_EMPTY) {
                                            } else {
                                                showImageToas(sendCodeBean.getMsg());
                                            }
                                        }
                                    }

                                    @Override
                                    public void onError(Response<String> response) {
                                        super.onError(response);
                                        dismissDialog();
                                    }
                                });
                            }
                        });
                    }
                } else {
                    head_photo.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (Utils.isDoubleClick()) {
                                return;
                            }
                            Bundle bundle1 = new Bundle();
                            bundle1.putString("userId", item.getByUserId() + "");
                            bundle1.putString("userName", item.getNickname() + "");
                            startActivity(OtherInfoActivity.class, bundle1);
                        }
                    });
                    item_ll.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (Utils.isDoubleClick()) {
                                return;
                            }
                            Bundle bundle1 = new Bundle();
                            bundle1.putString("userId", item.getByUserId() + "");
                            bundle1.putString("userName", item.getNickname() + "");
                            startActivity(OtherInfoActivity.class, bundle1);
                        }
                    });
                    if (item.getIsAttention() == 0) {
                        follow.setText("关注TA");
                        follow.setBackgroundResource(R.drawable.shape_yellow_oval);

                        follow.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (Utils.isDoubleClick()) {
                                    return;
                                }
                                showDialog(context, "关注中...");
                                finalOkGo.requestHead(RequestType.GETHEAD, Url.FOLLOW_OTHER + item.getByUserId(), null, Url.FOLLOW_OTHER, new Callback<SendCodeBean>() {
                                    @Override
                                    public void onSuccess(SendCodeBean sendCodeBean) {
                                        dismissDialog();
                                        if (sendCodeBean.getCode() == Contacts.SUCCESS) {
                                            item.setIsAttention(1);
                                            quickAdapter.notifyDataSetChanged();
                                            EventBus.getDefault().post("refresh_home_follow");
                                            EventBus.getDefault().post("refresh_mine");
                                        } else {
                                            if (sendCodeBean.getCode() == Contacts.TOKEN_EMPTY) {
                                            } else {
                                                showImageToas(sendCodeBean.getMsg());
                                            }
                                        }
                                    }

                                    @Override
                                    public void onError(Response<String> response) {
                                        super.onError(response);
                                        dismissDialog();
                                    }
                                });
                            }
                        });

                    } else {
                        follow.setText("已关注");
                        follow.setBackgroundResource(R.drawable.shape_gray_oval);

                        follow.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (Utils.isDoubleClick()) {
                                    return;
                                }
                                showDialog(context, "取消关注...");
                                finalOkGo.requestHead(RequestType.GETHEAD, Url.CANCEL_FOLLOW + item.getByUserId(), null, Url.CANCEL_FOLLOW, new Callback<SendCodeBean>() {
                                    @Override
                                    public void onSuccess(SendCodeBean sendCodeBean) {
                                        dismissDialog();
                                        if (sendCodeBean.getCode() == Contacts.SUCCESS) {
                                            item.setIsAttention(0);
                                            quickAdapter.notifyDataSetChanged();
                                            EventBus.getDefault().post("refresh_home_follow");
                                            EventBus.getDefault().post("refresh_mine");
                                        } else {
                                            if (sendCodeBean.getCode() == Contacts.TOKEN_EMPTY) {
                                            } else {
                                                showImageToas(sendCodeBean.getMsg());
                                            }
                                        }
                                    }

                                    @Override
                                    public void onError(Response<String> response) {
                                        super.onError(response);
                                        dismissDialog();
                                    }
                                });
                            }
                        });
                    }
                }
            }
        };

        listview.setAdapter(quickAdapter);

    }

    @Override
    protected void initData() {
        showDialog(context, null);
        if (flag.equals("mine_follow") || flag.equals("other_follow")) {
            HttpParams params = new HttpParams();
            if (flag.equals("other_follow")) {
                params.put("userId", userid);
            } else {
                params.put("userId", mUserInfoBean.getUser().getUserId());
            }
            params.put("limit", pageSize);
            params.put("page", pageNum++);
            if (flag.equals("mine_follow")) {
                params.put("type", 1);
            } else {
                params.put("type", 2);
            }
            params.put("loginUserId", mUserInfoBean.getUser().getUserId());
            finalOkGo.request(RequestType.POST, Url.WATCH_USER_FOLLOW_LIST, params, Url.WATCH_USER_FOLLOW_LIST, new Callback<FollowFansBean>() {
                @Override
                public void onSuccess(FollowFansBean followFansBean) {
                    dismissDialog();
                    if (followFansBean.getCode() == Contacts.SUCCESS) {
                        if (pageNum - 1 == 1) {
                            if (followFansBean.getPage().getList() == null || followFansBean.getPage().getList().size() == 0) {
                                if (flag.equals("mine_follow")) {
                                    empty_img.setVisibility(View.VISIBLE);
                                    empty_img.setImageResource(R.mipmap.icon_no_follow);
                                }
                            } else {
                                empty_img.setVisibility(View.GONE);
                            }
                            quickAdapter.clear();
                            quickAdapter.addAll(followFansBean.getPage().getList());
                        } else {
                            quickAdapter.addAll(followFansBean.getPage().getList());
                        }

                        refreshLayout.finishRefresh();
                        refreshLayout.finishLoadMore();
                        quickAdapter.notifyDataSetChanged();

                        int pagecount;
                        if (followFansBean.getPage().getTotalCount() % pageSize == 0) {
                            pagecount = followFansBean.getPage().getTotalCount() / pageSize;
                        } else {
                            pagecount = followFansBean.getPage().getTotalCount() / pageSize + 1;
                        }

                        if (pageNum > pagecount) {
                            refreshLayout.setNoMoreData(true);
                            refreshLayout.finishLoadMoreWithNoMoreData();
                        } else {
                            refreshLayout.setNoMoreData(false);
                        }
                    } else {
                        if (followFansBean.getCode() == Contacts.TOKEN_EMPTY) {
                        } else {
                            showImageToas(followFansBean.getMsg());
                        }
                    }

                }

                @Override
                public void onError(Response<String> response) {
                    super.onError(response);
                    dismissDialog();
                    if (pageNum - 1 == 1) {
                        empty_img.setVisibility(View.VISIBLE);
                        empty_img.setImageResource(R.mipmap.icon_no_net);
                    }
                }
            });
        } else if (flag.equals("mine_fans") || flag.equals("other_fans")) {
            HttpParams params = new HttpParams();
            if (flag.equals("other_fans")) {
                params.put("userId", userid);
            } else {
                params.put("userId", mUserInfoBean.getUser().getUserId());
            }
            params.put("limit", pageSize);
            params.put("page", pageNum++);
            params.put("loginUserId", mUserInfoBean.getUser().getUserId());
            finalOkGo.request(RequestType.POST, Url.WATCH_USER_FANS_LIST, params, Url.WATCH_USER_FANS_LIST, new Callback<FollowFansBean>() {
                @Override
                public void onSuccess(FollowFansBean followFansBean) {
                    dismissDialog();
                    if (followFansBean.getCode() == Contacts.SUCCESS) {
                        if (pageNum - 1 == 1) {
                            if (followFansBean.getPage().getList() == null || followFansBean.getPage().getList().size() == 0) {
                                if (flag.equals("mine_fans")) {
                                    empty_img.setVisibility(View.VISIBLE);
                                    empty_img.setImageResource(R.mipmap.icon_no_follow_msg);
                                }
                            } else {
                                empty_img.setVisibility(View.GONE);
                            }
                            quickAdapter.clear();
                            quickAdapter.addAll(followFansBean.getPage().getList());
                        } else {
                            quickAdapter.addAll(followFansBean.getPage().getList());
                        }

                        refreshLayout.finishRefresh();
                        refreshLayout.finishLoadMore();
                        quickAdapter.notifyDataSetChanged();

                        int pagecount;
                        if (followFansBean.getPage().getTotalCount() % pageSize == 0) {
                            pagecount = followFansBean.getPage().getTotalCount() / pageSize;
                        } else {
                            pagecount = followFansBean.getPage().getTotalCount() / pageSize + 1;
                        }

                        if (pageNum > pagecount) {
                            refreshLayout.setNoMoreData(true);
                            refreshLayout.finishLoadMoreWithNoMoreData();
                        } else {
                            refreshLayout.setNoMoreData(false);
                        }
                    } else {
                        refreshLayout.finishRefresh();
                        refreshLayout.finishLoadMore();
                        if (followFansBean.getCode() == Contacts.TOKEN_EMPTY) {
                        } else {
                            showImageToas(followFansBean.getMsg());
                        }
                    }

                }

                @Override
                public void onError(Response<String> response) {
                    super.onError(response);
                    dismissDialog();
                    refreshLayout.finishRefresh();
                    refreshLayout.finishLoadMore();
                    if (pageNum - 1 == 1) {
                        empty_img.setVisibility(View.VISIBLE);
                        empty_img.setImageResource(R.mipmap.icon_no_net);
                    }
                }
            });
        }

    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        initData();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        pageNum = 1;
        initData();
    }
}
